<template>
  <div class="singleImg-container-div">
    <template>
      <div class="el-upload-list el-upload-list--picture-card">
        <template v-if="!isHead && imageUrl.length > 0">
          <li
            class="el-upload-list__item is-success"
            v-for="(item, index) in imageUrl"
            :key="item"
          >
            <el-image
              :src="define.comUrl + item"
              class="el-upload-list__item-thumbnail"
              :preview-src-list="fileList"
              :z-index="10000"
              :ref="'image' + index"
            >
            </el-image>
            <span class="el-upload-list__item-actions">
              <span
                class="el-upload-list__item-preview"
                @click="handlePictureCardPreview('image' + index)"
              >
                <i class="el-icon-zoom-in"></i>
              </span>
              <span
                v-if="!disabled"
                class="el-upload-list__item-delete"
                @click="handleRemove(index)"
              >
                <i class="el-icon-delete"></i>
              </span>
            </span>
          </li>
        </template>
        <div
          v-if="!isHead && imageUrl.length < limit"
          class="upload_style"
          style="height: 100px; width: 100px"
          @click="uploadPicture('flagImg')"
        >
          <i class="el-icon-plus" style="font-size: 30px"></i>
          <div class="tip_word">{{ tip }}</div>
        </div>

        <div
          v-if="isHead"
          @click="uploadPicture('flagImg')"
          class="avatar-uploader"
        >
          <div class="avatar-box">
            <el-avatar
              :size="50"
              :src="
                value && value.substring(0, 4) == 'http'
                  ? value
                  : define.comUrl + value
              "
              class="avatar"
              v-if="value"
            />
            <div class="avatar-hover">更换头像</div>
          </div>
        </div>
      </div>
    </template>
    <!-- <el-dialog
      title="裁切图片"
      v-if="cropperModel"
      :visible.sync="cropperModel"
      :modal-append-to-body="false"
      :append-to-body="true"
      width="1200px"
      center
    > -->
    <cropper-image
      :Name="cropperName"
      :fixed="fixed"
      :autoCropWidth="autoCropWidth"
      :autoCropHeight="autoCropHeight"
      :fixedBox="fixedBox"
      :optionData="option"
      @uploadImgSuccess="handleUploadSuccess"
      ref="cropperImage"
    >
    </cropper-image>
    <input
      v-show="false"
      v-if="fileBtn"
      type="file"
      id="uploads"
      ref="uploadFile21"
      action=""
      style="position: absolute; clip: rect(0 0 0 0)"
      accept="image/png, image/jpeg, image/gif, image/jpg"
      @change="selectImg($event)"
    />
    <!-- </el-dialog> -->
  </div>
</template>

<script>
import CropperImage from "../../../CropperImage/CropperImage.vue";
export default {
  components: {
    CropperImage,
  },
  name: "UploadImage",
  props: {
    value: {
      type: String,
      default: "",
    },
    modular: {
      type: String,
      default: "system",
    },
    type: {
      type: String,
      default: "annex",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    limit: {
      type: Number,
      default: 1,
    },
    autoCropWidth: {
      type: Number,
      default: 300,
    },
    autoCropHeight: {
      type: Number,
      default: 200,
    },
    fixed: {
      type: Boolean,
      default: false,
    },
    fixedBox: {
      type: Boolean,
      default: false,
    },
    tip: {
      type: String,
      default: "图片上传",
    },
    isHead: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      fileBtn: true,
      imageUrl: [],
      uploadHeaders: { Authorization: this.$store.getters.token },
      cropperName: "",
      cropperModel: false,
      option: {
        img: "",
        outputType: "",
      },
    };
  },
  computed: {
    fileList() {
      let list = [];
      this.imageUrl.forEach((item) => {
        list.push(this.define.comUrl + item);
      });
      return list;
    },
  },
  watch: {},
  created() {},
  methods: {
    selectImg(e) {
      let file = e.target.files[0];
      if (!/\.(jpg|jpeg|png|JPG|PNG)$/.test(e.target.value)) {
        this.$message({
          message: "图片类型要求：jpeg、jpg、png",
          type: "error",
        });
        return false;
      }
      //转化为blob
      let reader = new FileReader();
      reader.onload = (e) => {
        let data;
        if (typeof e.target.result === "object") {
          data = window.URL.createObjectURL(new Blob([e.target.result]));
        } else {
          data = e.target.result;
        }
        this.option.img = data;
        let parts = data.split(";");
        let info = parts[0].split(":");
        this.option.outputType = info[1].split("/")[1];
        // this.fileBtn = false;
        console.log(this.option.outputType);
        this.$refs.uploadFile21.value = "";
        this.$refs.cropperImage.init();
        // this.fileBtn = true;
      };
      //转化为base64
      reader.readAsDataURL(file);
    },
    uploadPicture(name) {
      this.cropperName = name;
      this.cropperModel = true;
      this.$refs.uploadFile21.click();
    },
    //图片上传成功后
    handleUploadSuccess(obj) {
      console.log(obj, "------");
      if (this.isHead) {
        this.imageUrl = [];
      }
      this.imageUrl.push(obj.url);
      console.log(this.imageUrl, "1-+========");
      this.$emit("input", this.imageUrl);
      this.cropperModel = false;
    },

    handleSuccess(res, file) {
      if (res.code == 200) {
        this.imageUrl.push(res.data.url);
        // this.$emit("input", res.data.url);
      } else {
        this.$message({ message: res.msg, type: "error", duration: 1500 });
      }
    },
    handlePictureCardPreview(refName) {
      console.log(this.$refs[refName], "-----");
      this.$refs[refName] && this.$refs[refName][0].clickHandler();
    },
    handleRemove(index) {
      // this.$emit("input", "");
      // this.imageUrl = []
      this.imageUrl.splice(index, 1);
    },
  },
};
</script>
<style lang="scss" scoped>
.singleImg-container-div {
  // width: 100px;
  // height: 100px;
  cursor: pointer;
  .img-uploader {
    >>> .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;

      &:hover {
        border-color: #409eff;
      }
    }
  }

  .icon-box {
    position: relative;

    .upload-tip {
      position: absolute;
      font-size: 14px;
      color: #8c939d;
      left: 0;
      right: 0;
      padding: 0 5px;
      bottom: 10px;
      line-height: 20px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      word-break: break-all;
    }
  }

  .img-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 80px;
    text-align: center;
  }

  .el-upload-list {
    // width: 100px;
    height: 100px;
    // display: block;
    float: left;
    // margin-right: 20px;
    .el-upload-list__item {
      margin-right: 20px;
    }
    .el-upload-list__item,
    .el-image {
      width: 100px;
      height: 100px;
      float: left;
    }
  }
}
.upload_style {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  // display: inline-block;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  border: 1px dashed #cccccc;
  border-radius: 5px;
  overflow: hidden;
  box-shadow: 0 0 1px #cccccc;
  position: relative;
  float: left;
  margin-right: 20px;
}
.upload_style:hover {
  border: 1px solid #69b7ed;
}
.upload_style i {
  margin: 5px;
}
.el-icon-plus {
  color: #8c939d;
  position: relative;
  top: -8px;
}
.tip_word {
  display: block;
  position: absolute;
  top: 63px;
  color: #8c939d;
}

.head {
  position: absolute;
  left: 10px;
  top: 10px;
  height: 70px;
  width: 160px;
  padding-top: 10px;

  >>> .avatar-uploader {
    display: inline-block;
    vertical-align: top;

    .avatar-hover {
      position: absolute;
      left: 0;
      top: 0;
      font-size: 12px;
      display: none;
      overflow: hidden;
      width: 50px;
      height: 50px;
      text-align: center;
      border-radius: 50%;
      line-height: 50px;
      color: #fff;
      cursor: pointer;
      background: rgba(0, 0, 0, 0.5);
    }

    &:hover {
      & .avatar-hover {
        display: block;
      }
    }
  }

  .avatar-box {
    position: relative;
  }

  .avatar {
    display: inline-block;
    width: 50px;
    height: 50px;
    overflow: hidden;
    border-radius: 50%;
    vertical-align: top;
    margin-right: 10px;
  }

  .username {
    line-height: 50px;
    font-size: 12px;
  }
}
</style>
